package com.zhz.employee.biz;

import com.alibaba.fastjson.JSONObject;
import com.zhz.common.tool.mybatisplus.base.core.contract.Page;
import com.zhz.common.utils.BeanMapUtils;
import com.zhz.common.utils.ConvertUtils;
import com.zhz.employee.contract.constant.EmployeeConstant;
import com.zhz.employee.contract.request.*;
import com.zhz.employee.contract.response.*;
import com.zhz.model.employee.contract.dto.EmployeePositiveDTO;
import com.zhz.model.employee.contract.dto.EmployeeResignationDTO;
import com.zhz.model.employee.contract.dto.UserCompanyJobsDTO;
import com.zhz.model.employee.contract.dto.UserCompanyPersonalDTO;
import com.zhz.model.employee.contract.param.EmployeeArchiveQueryParam;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * 员工业务实现
 *
 * @author zhouhengzhe
 * @version 1.0
 * @date 2023/5/30 10:28
 */
@Slf4j
@Component
public class EmployeeBizService {

    private final UserCompanyPersonalBizService userCompanyPersonalBizservice;

    private final UserCompanyJobsBizService userCompanyJobsBizService;

    private final EmployeeResignationBizService employeeResignationBizService;

    private final EmployeePositiveBizService employeePositiveBizService;

    @Autowired
    public EmployeeBizService(UserCompanyPersonalBizService userCompanyPersonalBizservice,
                              UserCompanyJobsBizService userCompanyJobsBizService,
                              EmployeeResignationBizService employeeResignationBizService,
                              EmployeePositiveBizService employeePositiveBizService) {
        this.userCompanyPersonalBizservice = userCompanyPersonalBizservice;
        this.userCompanyJobsBizService = userCompanyJobsBizService;
        this.employeeResignationBizService = employeeResignationBizService;
        this.employeePositiveBizService = employeePositiveBizService;
    }

    public void printPdf(EmployeePdfRequest employeePdfRequest, HttpServletResponse response) {
        //2.构造数据
        //a.用户详情数据
        UserCompanyPersonalDTO userCompanyPersonalDTO =
                userCompanyPersonalBizservice.findById(employeePdfRequest.getId());
        //b.用户岗位信息数据
        UserCompanyJobsDTO userCompanyJobsDto = userCompanyJobsBizService.findById(employeePdfRequest.getId());
        //c.用户头像(刷脸的信息) 域名 / id
        String staffPhoto = String.format(EmployeeConstant.CLOUD_DN, employeePdfRequest.getId());
        log.info("用户头像的url为：{}", staffPhoto);
        //3.填充pdf模板数据，并输出pdf
        Map<String, Object> map1 = BeanMapUtils.beanToMap(userCompanyPersonalDTO);
        Map<String, Object> map2 = BeanMapUtils.beanToMap(userCompanyJobsDto);
        ServletOutputStream os = null;
        try {
            //1.引入jasper文件
            Resource resource = new ClassPathResource(EmployeeConstant.PROFILE);
            FileInputStream fis = new FileInputStream(resource.getFile());
            os = response.getOutputStream();
            JasperPrint print = JasperFillManager.fillReport(
                    fis,
                    new JSONObject()
                            .fluentPutAll(map1)
                            .fluentPutAll(map2)
                            .fluentPut("staffPhoto", "staffPhoto"),
                    new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print, os);
        } catch (JRException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(os)) {
                try {
                    os.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }

    /**
     * 员工个人信息保存
     *
     * @param employeeSavePersonalRequest 员工保存个人信息请求
     * @param companyId                   公司ID
     */
    public void savePersonalInfo(EmployeeSavePersonalRequest employeeSavePersonalRequest, String companyId) {
        userCompanyPersonalBizservice.save(employeeSavePersonalRequest, companyId);

    }

    /**
     * 员工个人信息模板
     *
     * @param findPersonalInfoRequest 员工个人信息模板请求体
     * @return
     */
    public FindPersonalInfoResponse findPersonalInfo(FindPersonalInfoRequest findPersonalInfoRequest) {
        return userCompanyPersonalBizservice.findPersonalInfo(findPersonalInfoRequest);
    }

    /**
     * 员工岗位信息保存
     *
     * @param employeeSaveJobsInfoRequest 员工岗位信息保存请求
     * @param companyId                   公司ID
     */
    public void saveJobsInfo(EmployeeSaveJobsInfoRequest employeeSaveJobsInfoRequest, String companyId) {
        userCompanyJobsBizService.save(employeeSaveJobsInfoRequest, companyId);
    }

    /**
     * 员工岗位信息读取
     *
     * @param employeeFindJobsInfoRequest 主键ID
     * @param companyId                   公司ID
     * @return 员工岗位信息
     */
    public EmployeeFindInfoResponse findJobsInfo(EmployeeFindJobsInfoRequest employeeFindJobsInfoRequest, String companyId) {
        UserCompanyJobsDTO info = userCompanyJobsBizService.findInfoById(employeeFindJobsInfoRequest.getId(), companyId);
        return ConvertUtils.sourceToTarget(info, EmployeeFindInfoResponse.class);
    }

    /**
     * 离职
     *
     * @param employeeSaveLeaveRequest 员工离职请求
     */
    public void saveLeave(EmployeeSaveLeaveRequest employeeSaveLeaveRequest) {
        employeeResignationBizService.saveLeave(employeeSaveLeaveRequest);
    }

    /**
     * 离职表单读取
     *
     * @param employeeFindLeaveRequest 离职表单读取请求
     * @return 离职表单读取响应
     */
    public EmployeeFindLeaveResponse findLeave(EmployeeFindLeaveRequest employeeFindLeaveRequest) {
        EmployeeResignationDTO employeeResignationDTO = employeeResignationBizService.findLeave(employeeFindLeaveRequest);
        return ConvertUtils.sourceToTarget(employeeResignationDTO, EmployeeFindLeaveResponse.class);
    }

    /**
     * 调岗表单保存
     *
     * @param employeeTransferPositionRequest 调岗表单保存请求
     */
    public void saveTransferPosition(EmployeeTransferPositionRequest employeeTransferPositionRequest) {
        employeeResignationBizService.saveTransferPosition(employeeTransferPositionRequest);
    }

    /**
     * 调岗表单读取
     *
     * @param employeeFindTransferPositionRequest 调岗表单读取请求
     * @return 调岗表单读取响应
     */
    public EmployeeFindInfoResponse findTransferPosition(EmployeeFindTransferPositionRequest employeeFindTransferPositionRequest) {
        UserCompanyJobsDTO userCompanyJobsDTO = userCompanyJobsBizService.findInfoById(employeeFindTransferPositionRequest.getId(), null);
        return ConvertUtils.sourceToTarget(userCompanyJobsDTO, EmployeeFindInfoResponse.class);
    }

    /**
     * 转正表单保存
     *
     * @param employeePositiveSaveRequest 转正表单保存请求
     */
    public void savePositive(EmployeePositiveSaveRequest employeePositiveSaveRequest) {
        employeePositiveBizService.save(employeePositiveSaveRequest);
    }

    /**
     * 转正表单读取
     *
     * @param employeeFindPositiveRequest 转正表单读取请求
     * @return 转正表单读取响应
     */
    public EmployeeFindPositiveResponse findPositive(EmployeeFindPositiveRequest employeeFindPositiveRequest) {
        EmployeePositiveDTO employeePositiveDto = employeePositiveBizService.findPositive(employeeFindPositiveRequest);
        return ConvertUtils.sourceToTarget(employeePositiveDto, EmployeeFindPositiveResponse.class);
    }

    public Page<EmployeeArchiveResponse> findArchives(EmployeeArchiveQueryParam employeeArchiveQueryParam) {

        return null;
    }
}
